﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 第四次课堂作业
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;                 

            System.Timers.Timer t1 = new System.Timers.Timer(1000);        

            t1.Elapsed += new System.Timers.ElapsedEventHandler(Dida);             //每一秒重新调用Dida事件

            t1.AutoReset = true;

            t1.Enabled = true;
        }

        int didasum = 0;
        bool isDing = false;
        int dingsum = 0;
        public void Dida(object source,System.Timers.ElapsedEventArgs e)  //滴答事件
        {
            Rh.Text = DateTime.Now.Hour.ToString();
            Rm.Text = DateTime.Now.Minute.ToString();
            Rs.Text = DateTime.Now.Second.ToString();      //C#自带的获取时间的函数
            if (!isDing)
            {
                Stop.Visible = false;          // 是否可视
            }
            if (didasum > 9)
            {
                didasum = 0;
                DiDa.Text = "";
            }
            DiDa.Text += "滴答  ";
            didasum++;
        }

        public void Ding(object source, System.Timers.ElapsedEventArgs e)  //叮铃事件
        {
            string FH = Fh.Text;
            string FM = Fm.Text;
            string FS = Fs.Text;
            try
            {
                if (FH == "" || FM == "" || FS == "")
                {
                    isBegin = false;
                    throw new ArgumentNullException();
                }
                if (int.Parse(FH) < 0 || int.Parse(FH) > 23)
                {
                    isBegin = false;
                    throw new MyException("时：请输入0至23的整数\n");
                }
                if (int.Parse(FM) < 0 || int.Parse(FM) > 59)
                {
                    isBegin = false;
                    throw new MyException("分：请输入0至59的整数\n");
                }
                if (int.Parse(FS) < 0 || int.Parse(FS) > 59)
                {
                    isBegin = false;
                    throw new MyException("秒：请输入0至59的整数\n");
                }
                tip.Text = "闹铃启动中";
                //到时刻响铃
                if ((int.Parse(FH) == int.Parse(Rh.Text)) && (int.Parse(FM) == int.Parse(Rm.Text)) && (int.Parse(FS) == int.Parse(Rs.Text)))
                {
                    isDing = true;
                    Stop.Visible = true;
                    while (isDing)
                    {
                        Thread.Sleep(1000);
                        if (dingsum > 6)
                        {
                            dingsum = 0;
                            Dingding.Text = "";
                        }
                        Dingding.Text += "叮铃铃   ";
                        dingsum++;
                        if (!isDing) Dingding.Text = "";
                    }
                }
            }
            catch (ArgumentNullException ex) //处理输入为空异常
            {
                tip.Text = "时、分、秒输入不能为空！！！";
                t.Enabled = false;
                t.Elapsed -= new System.Timers.ElapsedEventHandler(Ding);
                isBegin = false;
            }
            catch (FormatException ex)  //处理输入类型错误异常
            {
                tip.Text = "时、分、秒请输入整数";
                t.Enabled = false;
                t.Elapsed -= new System.Timers.ElapsedEventHandler(Ding);
                isBegin = false;
            }
            catch (MyException myException)  //处理输入整数范围错误异常
            {
                tip.Text += myException.Message + "  ";
                t.Enabled = false;
                t.Elapsed -= new System.Timers.ElapsedEventHandler(Ding);
            }
        }

        bool isBegin = false;
        System.Timers.Timer t = new System.Timers.Timer(1000);
        private void Start_Click(object sender, EventArgs e)
        {
            if (!isBegin)
            {
                t.Elapsed += new System.Timers.ElapsedEventHandler(Ding);                   //对象运行，增加一个事件   
                t.AutoReset = true;                                                         //是否重复运行
                t.Enabled = true;                                                           //判断是否执行
                isBegin = true;
                tip.Text = "";
            }
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            t.Enabled = false;
            t.Elapsed -= new System.Timers.ElapsedEventHandler(Ding);
            isBegin = false;
            tip.Text = "";
        }

        private void Stop_Click(object sender, EventArgs e)
        {
            isDing = false;
            tip.Text = "";
            t.Elapsed -= new System.Timers.ElapsedEventHandler(Ding);
            isBegin = false;
            Stop.Visible = false;
        }
    }
    class MyException : Exception //自定义异常（限制输入数字在范围内）
    {
        public MyException(string message) : base(message)
        {
        }
    }
}
